/*
 * Timer.c 主控制程序中的计时功能
 */
#include "head\Timer.h"

void Timer_InitTimer()
{
//    TimerLowPowMode = 300;//初始每五分钟检查一次
//    TimerATGM336AntennaCheck.timer = 3;//初始每3秒检查一次
//    TimerATGM336Led.timer = 3;//每1秒转换一次led灯，但进入正常工作状态后3秒开始发送
    Timer_startTiming(&TimerVbatCheck,10);
    Timer_startTiming(&TimerATGM336AntennaCheck,3);
    Timer_startTiming(&TimerATGM336Led,3);

    Timer_A_initUpModeParam initUpParam = {0};
    initUpParam.clockSource = TIMER_A_CLOCKSOURCE_ACLK;
    initUpParam.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1;
    initUpParam.timerPeriod = 32768;
    initUpParam.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_DISABLE;
    initUpParam.captureCompareInterruptEnable_CCR0_CCIE =TIMER_A_CAPTURECOMPARE_INTERRUPT_DISABLE;
    initUpParam.timerClear = TIMER_A_DO_CLEAR;
    initUpParam.startTimer = false;
    Timer_A_initUpMode(MAIN_TIMER_BASE, &initUpParam);

    //Initiaze compare mode
    Timer_A_clearCaptureCompareInterrupt(MAIN_TIMER_BASE,
        MAIN_TIMER_CCR
        );

    Timer_A_initCompareModeParam initCompParam = {0};
    initCompParam.compareRegister = MAIN_TIMER_CCR;
    initCompParam.compareInterruptEnable = TIMER_A_CAPTURECOMPARE_INTERRUPT_ENABLE;
    initCompParam.compareOutputMode = TIMER_A_OUTPUTMODE_OUTBITVALUE;
    initCompParam.compareValue = 32768;
    Timer_A_initCompareMode(MAIN_TIMER_BASE, &initCompParam);

    Timer_A_startCounter(MAIN_TIMER_BASE,TIMER_A_UP_MODE);
}

void Timer_startTiming(strSecondChronograph *strSC,uint16_t seconds)
{
    uint16_t TimerS_now = TimerS.timer;
    strSC->seconds = seconds;
    strSC->timer = TimerS_now + strSC->seconds;//计算得到秒表计时结束时的TimerS
    strSC->flag &= ~TIMER_STR_SECOND_CHRON_FLAG_OVERFLOW;//溢出位标志清0
    strSC->flag |= TIMER_STR_SECOND_CHRON_FLAG_RUNING;//开启位标志置1

    if(strSC->timer < TimerS_now)//本次计时溢出
    {
        strSC->flag |= TIMER_STR_SECOND_CHRON_FLAG_OVERFLOW;//溢出位标志置1
    }
}

void Timer_closeTiming(strSecondChronograph *strSC)
{
    memset(strSC,0,sizeof(strSecondChronograph));
}

uint8_t Timer_checkoutTiming(strSecondChronograph *strSC)
{
    if((strSC->flag & TIMER_STR_SECOND_CHRON_FLAG_OVERFLOW) == 0)//本次计时没有溢出
    {
        if(TimerS.timer >= strSC->timer)//如果秒表超越了计时器的数值，则表示计时时间到
        {
            Timer_closeTiming(strSC);
            return 1;
        }
    }
    else//本次计时溢出
    {
        if(TimerS.timer <= (65535 - strSC->seconds))//判断计时是否已经溢出,从0开始
        {
            if(TimerS.timer >= strSC->timer)//如果秒表超越了计时器的数值，则表示计时时间到
            {
                Timer_closeTiming(strSC);
                return 1;
            }
        }
    }
    return 0;
}

//******************************************************************************
//
//This is the TIMER0_A0 interrupt vector service routine.
//
//******************************************************************************
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=TIMER0_A0_VECTOR
__interrupt
#elif defined(__GNUC__)
__attribute__((interrupt(TIMER1_A0_VECTOR)))
#endif
void TIMER0_A0_ISR (void)
{
    TimerS.timer++;
    if(TimerLowPowMode.flag & TIMER_STR_SECOND_CHRON_FLAG_RUNING)
    {
        if(Timer_checkoutTiming(&TimerLowPowMode))
        {
            __bic_SR_register_on_exit(LPM0_bits);
        }
    }
    else
    {
        P3OUT ^= BIT6;//MCU工作状态指示灯闪烁
    }
}
